package com.ssl.admin.service;


import com.ssl.admin.pojo.CommodityBrand;
import com.ssl.admin.repo.CommodityBrandRepository;
import com.ssl.admin.repo.CommodityCategoryBrandRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class CommodityBrandService {
    @Autowired
    private CommodityBrandRepository commodityBrandRepository;

    @Autowired
    private CommodityCategoryBrandRepository commodityCategoryBrandRepository;

    public void create(CommodityBrand brand) {
        Date date = new Date();
        brand.setCreateTime(date);
        brand.setUpdateTime(date);
        commodityBrandRepository.save(brand);
    }

    @Transactional
    public void delete(Long id) {
        commodityBrandRepository.deleteById(id);
        commodityCategoryBrandRepository.deleteByBrandId(id);
    }

    public void update(CommodityBrand brand) {
        CommodityBrand commodityBrand = commodityBrandRepository.getById(brand.getId());

        brand.setCreateTime(commodityBrand.getCreateTime());
        brand.setUpdateTime(new Date());
        commodityBrandRepository.save(brand);
    }

    public Object getListByCategoryId(Long categoryId) {
        List<CommodityBrand> brandList = commodityBrandRepository.getByCategoryId(categoryId);
        return brandList;
    }
}
